
function changesrc (obj) {
    var big_src = document.getElementById('big');
    big_src.src = obj.src.replace('50_','350_');
}

//是商品购买数量不小于0
$("#qty").blur(function(){
    var qty = $("#qty").val();
    var max = Number($("input[name='num']").val());
    if(qty <=0){
        $("#qty").val(1);
    }else if(qty > max){
        alert("库存不足");
        $("#qty").val(max);
    }
})

function plus(){
    var qty = $("#qty").val();
    var max = Number($("input[name='num']").val());
    qty = Number(qty) + 1;
    //如果购买数量超过库存
    if(qty > max){
        alert("库存不足");
        $("#qty").val(max);
    }else{
        $("#qty").val(qty);
    }
}

function minus(){
    var qty = $("#qty").val();
    qty = Number(qty) - 1;
    if(qty <= 0){
        $("#qty").val(1);
    }else{
        $("#qty").val(qty);
    }
}
//购物车中的添加地址模块显示
function addAddress (){
    $("#addAddress").show();
}
//购物车中的添加地址模块隐藏,并把数据加入到数据库
function add(){
    $("#addAddress").hide();
    var content = $("#address").val();
    var tel = $("#tel").val();
    var name = $("#name").val();
    $.ajax({
        type: "get",
        url: "addAddress",
        data: "content="+content+"&tel="+tel+"&name="+name,
        success: function(){
            showAddress ();
        }
    })
}
//购物车页面的地址显示,一进入购物车页面就自动调用
function showAddress (){
    $.ajax({
        type : "get",
        url : "showAddress",
        dataType : "json",
        success : function(data){
            $("#newaddress").html("");
            for(var i=0; i<data.length;i++){
                if(data[i].is_default == 1){
                    var content1 = '<div class="radio col-md-12"><label>';
                    var content2 = '<input type="radio" name="address" value="'+data[i].id+'" checked>'+data[i].address;
                    var content3 = '</label></div>';
                    $("#newaddress").append(content1+content2+content3);
                    continue;
                }else{

                    var content1 = '<div class="radio col-md-12"><label>';
                    var content2 = '<input type="radio" name="address" value="'+data[i].id+'">'+data[i].address;
                    var content3 = '</label></div>';
                    var content4 = '&nbsp;&nbsp;&nbsp;&nbsp;<a onclick="default_address(this)" style="color: blue">设为默认地址</a>'
                    $("#newaddress").append(content1+content2+content4+content3);
                }


            }

        }
    })
}
//购物车默认地址设置
function default_address(obj) {
    var id = $(obj).siblings().val();
    $.ajax({
        type : "get",
        url : "setDefault",
        data : "id="+id,
        success : function(data){
            if(data == 1){
                showAddress();

            }else{
                alert("设置默认地址失败");
            }

        }
    })
}

//购物车中的商品数量减
function minusCart(id) {

    //给当前的商品数量减1
    var number = document.getElementById('qty'+id);
    number.value = parseInt(number.value) - 1;
    if(number.value <= 0){
        number.value = 1;
    }
    //商品价格
    var price = document.getElementById('price'+id);
    //小计
    var total = document.getElementById('total'+id);
    total.innerHTML = number.value * price.innerHTML;
    updataCart (number.value,id);
    allSum();
}
//购物车中的商品数量加
function plusCart(id){
    //最大库存
    var max = document.getElementById('num'+id).value;
    //给当前的商品数量加1
    var number = document.getElementById('qty'+id);
    number.value = parseInt(number.value) + 1;
    if(number.value >= parseInt(max)){
        number.value = parseInt(max);
    }
    //商品价格
    var price = document.getElementById('price'+id);
    //小计
    var total = document.getElementById('total'+id);
    total.innerHTML = number.value * price.innerHTML;
    updataCart (number.value,id);
    allSum();
}
//直接更改购物车页面商品数量时,调用
function change_num_cart (id){
    //最大库存
    var max = document.getElementById('num'+id).value;
    //当前数量
    var number = document.getElementById('qty'+id);
    //小于1时,就为1,大于库存时,就为库存数量
    if(number.value <= 0){
        number.value = 1;
    }else if(number.value >= parseInt(max)){
        number.value = parseInt(max);
    }
    //商品价格
    var price = document.getElementById('price'+id);
    //小计
    var total = document.getElementById('total'+id);
    total.innerHTML = number.value * price.innerHTML;
    updataCart (number.value,id);
    allSum();
}

//购物车中的商品数量发生变动时,就直接把数量更新到购物车
function updataCart (number,id){

    $.ajax({
        type : "get",
        url : "updateCart",
        data : "num="+number+"&goods_id="+id,
    })
}

//购物车总价格
function allSum (){
    var sumz = 0;
    $("input[id^='box_']").each(function(){
        if(this.checked){
            var id = this.id;
            id = id.replace("box_","total");
            var total = document.getElementById(id).innerHTML;
            sumz+=Number(total);
        }
    })
    //alert(sumz)
    $("#total_price").text(sumz);
}
//把所有选中的商品id放到表单中
function allGoods(){
    var goodsId = [];
    $("input[id^='box_']").each(function(){
        if(this.checked) {
            var id = this.id;
            id = id.replace("box_","");
            goodsId.push(id);
        }
    })
    $("#allGoodsId").val(goodsId);
}

//确认订单页面的总价格计算
function totalPrice(){
    //一进入页面就计算总钱数
    var total = 0;
    $("span[class^='order_']").each(function(){
        total += parseInt($(this).html());
    })
    console.log(total);
    //减免优惠卷
    $("option").each(function () {
        if(this.selected){
            if($(this).val() > 0){
                total = total - parseInt($(this).next().val());
                console.log($(this).val());
            }
        }
    })
    //减免积分
    changeIntegral ();
    $("input[name='inte']").each(function () {
        if(this.checked){
            if(this.value == 1){
                total = total - $("#integralPrice").html();
                console.log($("#integralPrice").html());
            }
        }

    })
    $("#total_price").html(total);

}


//将积分转化为抵扣钱数
function changeIntegral (){
    var num = $("#integralNum").html();
    if(parseInt(num) > 0 ){
        var price = parseInt(num)/100;
        $("#integralPrice").html(price);
    }

}
$(function(){
    $("#checkAll").click(function(){
        if(this.checked){
            $("input[type='checkbox']").prop('checked','checked');
        }else{
            $("input[type='checkbox']").removeAttr('checked');
        }
        allSum();
        allGoods();
    })

    $("input[id^='box_']").each(function(){
        $(this).click(function(){
            allSum();
            allGoods();
        })

    })
})